package Observer.Stock;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractStockPrice {
    protected float price, oldPrice;
    protected String code;
    protected List<IStockHolder> stockHolderList;

    public AbstractStockPrice(String code)
    {
        this.code = code;
        price = oldPrice = 0;
        stockHolderList = new ArrayList<IStockHolder>();
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
        notifyHolder();
    }

    public float getOldPrice() {
        return oldPrice;
    }

    public void setOldPrice(float oldPrice) {
        this.oldPrice = oldPrice;
    }

    public void add(IStockHolder ish) {
        stockHolderList.add(ish);
    }

    public void remove(IStockHolder ish)
    {
        stockHolderList.remove(ish);
    }

    protected abstract void notifyHolder();

    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        return result;
    }

    public boolean equals(Object obj)
    {
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(getClass() != obj.getClass())
            return false;
        AbstractStockPrice other = (AbstractStockPrice) obj;
        if(code == null)
        {
            if(other.code != null)
                return false;
        }
        else if(!code.equals(other.code))
            return false;
        return true;
    }



}
